/*
* @(#)ListUserMemberAction.java
*
* Copyright (c) 2003 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
*/
package com.dcivision.user.web;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.dms.bean.DmsLocMaster;
import com.dcivision.dms.core.RootRetrievalManager;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.MessageResourcesFactory;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractListAction;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.user.UserErrorConstant;
import com.dcivision.user.bean.MtmUserGroupUserRole;
import com.dcivision.user.bean.MtmUserRecordUserGroup;
import com.dcivision.user.bean.MtmUserRecordUserRole;
import com.dcivision.user.bean.UserGroup;
import com.dcivision.user.bean.UserMember;
import com.dcivision.user.bean.UserRecord;
import com.dcivision.user.dao.MtmUserGroupUserRoleDAObject;
import com.dcivision.user.dao.MtmUserRecordUserGroupDAObject;
import com.dcivision.user.dao.MtmUserRecordUserRoleDAObject;
import com.dcivision.user.dao.UserGroupDAObject;
import com.dcivision.user.dao.UserRecordDAObject;
import com.dcivision.user.dao.UserRoleDAObject;

/**
 * <p>Class Name:       ListUserMemberAction.java    </p>
 * <p>Description:      The list action class for ListUserRole.jsp</p>
 *
 *    @author           Jenny Li
 *    @company          DCIVision Limited
 *    @creation date    26/10/2004
 *    @version          $Revision: 1.14.2.1 $
 */
/* ********************************Modify History***********************************************
 * Modify by  : Jicle.Wang 
 * Modify date: 2006/07/04
 * BugID      : 5309
 * Description: Method getListData() - retrieve the appropriate item list for current page
 * *********************************************************************************************
 */
public class ListUserMemberAction extends AbstractListAction {

  public static final String REVISION = "$Revision: 1.14.2.1 $";
  
  protected final static String MESSAGE_RECORD_INSERT = "common.message.record_inserted";
  protected static String MESSAGE_RECORDS_REMOVED = "user.message.records_removed";

  /**
   *  Constructor - Creates a new instance of ListUserRoleAction and define the default listName.
   */
  public ListUserMemberAction() {
    super();
    this.setListName("userMemberList");
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  
  public String getMajorDAOClassName() {
    return("com.dcivision.user.dao.MtmUserRecordUserRoleDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  
  public String getFunctionCode() {
    return(SystemFunctionConstant.SETUP_USER_ROLE);
  }
  
  /**
   *  Method execute() - generaic function of result list handling, including:
   *                     1. Delete record in List
   *                     2. Get List Data
   *
   *  @param      mapping               ActionMapping from struts
   *  @param      form                  ActionForm from struts
   *  @param      request               HttpServletReuqest
   *  @param      respond               HttpServletRespond
   *  @return     ActionForward         Return the action forward object for struts
   *  @throws     ServletException      Throws ServletException
   */
  
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
    ListUserMemberForm maintForm=(ListUserMemberForm)form;
    String opMode = (maintForm != null) ? maintForm.getOpMode() : request.getParameter("opMode"); //maintForm.getOpMode();
    String navMode = (maintForm != null) ? maintForm.getNavMode() : request.getParameter("navMode"); //maintForm.getNavMode();
    ActionForward forward = this.retrieveFunctionCode(request, response, mapping);
    if (forward != null) {
      return forward;
    } 
    AbstractSearchForm searchForm=(AbstractSearchForm)form;
    if(UserMember.OPMODE_ADD_USER.equals(maintForm.getOpMode())){
      try{
        this.addUserRecord(mapping,form,request,response);
        this.commit(request);
        this.getListData(mapping,searchForm,request,response);
        String error=(String)request.getAttribute("error");
        if(!Utility.isEmpty(error)){
          addError(request,error);  
        }else{
          addMessage(request,MESSAGE_RECORD_INSERT); 
        }
      }catch (ApplicationException appEx) {
        this.rollback(request);
        handleApplicationException(request, appEx);
      }
      maintForm.setUserRecordID("");
      maintForm.setNavMode(GlobalConstant.NAV_MODE_LIST);
      return mapping.findForward(GlobalConstant.NAV_MODE_LIST);
    }else if(UserMember.OPMODE_ADD_GROUP.equals(maintForm.getOpMode())){
      try{
        this.addUserGroup(mapping,form,request,response);
        this.commit(request);
        this.getListData(mapping,searchForm,request,response);
        String error=(String)request.getAttribute("error");
        if(!Utility.isEmpty(error)){
          addError(request,error);  
        }else{
          addMessage(request,MESSAGE_RECORD_INSERT); 
        }
      }catch (ApplicationException appEx) {
        this.rollback(request);
        handleApplicationException(request, appEx);
      }
      maintForm.setOpMode("");
      maintForm.setUserGroupID("");
      maintForm.setNavMode(GlobalConstant.NAV_MODE_LIST);
      return mapping.findForward(GlobalConstant.NAV_MODE_LIST);
    }else if("REMOVE.OPTION".equals(opMode)){
      try{
        this.removeRecord(mapping,form,request,response);
        this.commit(request);
        addMessage(request, MESSAGE_RECORDS_REMOVED, ""+searchForm.getBasicSelectedID().length);
        searchForm.setBasicSelectedID(null);
        searchForm.setOpMode(null);
        this.getListData(mapping,searchForm,request,response);
        
        List resultList = (List)request.getAttribute(this.m_sListName);
        while ( (resultList.size()==0) && (TextUtility.parseInteger(searchForm.getCurStartRowNo())>TextUtility.parseInteger(searchForm.getPageOffset())) ) {
          int prvRowNo = TextUtility.parseInteger(searchForm.getCurStartRowNo())-TextUtility.parseInteger(searchForm.getPageOffset());
          searchForm.setCurStartRowNo(TextUtility.formatInteger(prvRowNo));
          this.getListData(mapping, searchForm, request, response);
        }
        
      }catch(ApplicationException appEx){
        this.rollback(request);
        handleApplicationException(request,appEx);
      }
      return mapping.findForward(GlobalConstant.NAV_MODE_LIST);
    }else if("EXCEL_REPORT".equals(opMode)){
      this.getExcelReport(mapping,searchForm,request,response);
      this.getListData(mapping,searchForm,request,response);
      return mapping.findForward(GlobalConstant.NAV_MODE_LIST);
    }else{
//      return super.execute(mapping,form,request,response);   
    	ActionForward actionForward = super.execute(mapping,form,request,response);
    	List resultList = (List)request.getAttribute(this.m_sListName);
      while ( (resultList.size()==0) && (TextUtility.parseInteger(searchForm.getCurStartRowNo())>TextUtility.parseInteger(searchForm.getPageOffset())) ) {
        int prvRowNo = TextUtility.parseInteger(searchForm.getCurStartRowNo())-TextUtility.parseInteger(searchForm.getPageOffset());
        searchForm.setCurStartRowNo(TextUtility.formatInteger(prvRowNo));
        this.getListData(mapping, searchForm, request, response);
      }
      return actionForward;
    }
  }
  
  
  public void getListData(ActionMapping mapping, AbstractSearchForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    String userRecordIDs = "";
    String userGroupIDs = "";
  	SessionContainer sessionContainer=this.getSessionContainer(request);
    Connection conn=this.getConnection(request);
  	ListUserMemberForm listUserForm=(ListUserMemberForm)form;
    MtmUserRecordUserRoleDAObject mtmUserRecordUserRoleDAO=new MtmUserRecordUserRoleDAObject(sessionContainer,conn);
    MtmUserGroupUserRoleDAObject mtmUserGroupUserRoleDAO=new MtmUserGroupUserRoleDAObject(sessionContainer,conn);
    MtmUserRecordUserGroupDAObject mtmUserRecordUserGroupDAO=new MtmUserRecordUserGroupDAObject(sessionContainer,conn);
    UserRecordDAObject userRecordDAO=new UserRecordDAObject(sessionContainer,conn);
    UserGroupDAObject userGroupDAO=new UserGroupDAObject(sessionContainer,conn);
    UserRoleDAObject userRoleDAO=new UserRoleDAObject(sessionContainer,conn);
    List memberList = null;
    if(UserMember.MEMBER_TYPE_ROLE.equals(listUserForm.getMemberType())){
      memberList = userRecordDAO.getGroupAndRecordByRoleID(listUserForm);
    }else if(UserMember.MEMBER_TYPE_GROUP.equals(listUserForm.getMemberType())){
    	memberList = userRecordDAO.getRecordByRoleID(listUserForm);
    }    
    
    for(int i=0;i<memberList.size();i++){
    	UserMember um = (UserMember)memberList.get(i);
    	if(um.getMemberType().equals("U")){
    		userRecordIDs += TextUtility.formatIntegerObj(um.getID()) + ",";     			
    	}else if(um.getMemberType().equals("G")){
    		userGroupIDs += TextUtility.formatIntegerObj(um.getID()) + ","; 
    	}
    }
    
    if(!userRecordIDs.equals("")) {
      userRecordIDs = userRecordIDs.substring(0, userRecordIDs.length()-1);
    }
    if(!userGroupIDs.equals("")) {
      userGroupIDs = userGroupIDs.substring(0, userGroupIDs.length()-1);
    }
    
    log.info("userRecordIDs:" + userRecordIDs);
    log.info("userGroupIDs:" + userGroupIDs);
    request.setAttribute("mulitpleRecordValue",userRecordIDs);
    request.setAttribute("mulitpleGroupValue",userGroupIDs);
    
    if(memberList.size()>0){      
      request.setAttribute(this.getListName(),memberList);
    }else{
      request.setAttribute(this.getListName(),new ArrayList());   
    }  
    
  }
  /* Jicle.Wang 5309 2006/07/04 End   */
  /**
   * addUserRecord to group or role
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @throws ApplicationException
   */
  public void addUserRecord(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    SessionContainer sessionContainer=this.getSessionContainer(request);
    Connection conn=this.getConnection(request);
    ListUserMemberForm memberForm=(ListUserMemberForm)form;
    if(!Utility.isEmpty(memberForm.getUserRecordID())){
      String[] userRecordIDArray = TextUtility.splitString(memberForm.getUserRecordID(), ",");
      //Integer userRecordID=TextUtility.parseIntegerObj(memberForm.getUserRecordID());
      Integer userRoleID=TextUtility.parseIntegerObj(memberForm.getUserRoleID());
      Integer userGroupID=TextUtility.parseIntegerObj(memberForm.getUserGroupID());
      for (int i=0;i<userRecordIDArray.length;i++){
      	Integer userRecordID=TextUtility.parseIntegerObj(userRecordIDArray[i]);
        if(UserMember.MEMBER_TYPE_ROLE.equals(memberForm.getMemberType())){
          MtmUserRecordUserRoleDAObject mtmUserRecordUserRoleDAO=new MtmUserRecordUserRoleDAObject(sessionContainer,conn);
          MtmUserRecordUserRole mtmUserRecordUserRole=new MtmUserRecordUserRole();
          mtmUserRecordUserRole.setUserRecordID(userRecordID);
          mtmUserRecordUserRole.setUserRoleID(userRoleID);
          List list=mtmUserRecordUserRoleDAO.getListByUserRecordID(userRecordID,userRoleID);
          if(list.size()>0){
            request.setAttribute("error",UserErrorConstant.DUPLICATE_RECORD); 
          }else{
            mtmUserRecordUserRoleDAO.insertObject(mtmUserRecordUserRole);
          }
        }else if(UserMember.MEMBER_TYPE_GROUP.equals(memberForm.getMemberType())){
          MtmUserRecordUserGroupDAObject mtmUserRecordUserGroupDAO=new MtmUserRecordUserGroupDAObject(sessionContainer,conn); 
          MtmUserRecordUserGroup mtmUserRecordUserGroup=new MtmUserRecordUserGroup();
          mtmUserRecordUserGroup.setUserRecordID(userRecordID);
          mtmUserRecordUserGroup.setUserGroupID(userGroupID);
          List list=mtmUserRecordUserGroupDAO.getListByUserRecordID(userRecordID,userGroupID);
          if(list.size()>0){
            request.setAttribute("error",UserErrorConstant.DUPLICATE_RECORD); 
          }else{
            mtmUserRecordUserGroupDAO.insertObject(mtmUserRecordUserGroup);
          }
        }
      }
    }
  }
  
  /**
   * add group to role or add record to group
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @throws ApplicationException
   */
  public void addUserGroup(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    SessionContainer sessionContainer=this.getSessionContainer(request);
    Connection conn=this.getConnection(request);
    ListUserMemberForm memberForm=(ListUserMemberForm)form;
    if(!Utility.isEmpty(memberForm.getUserGroupID())){
      Integer userRecordID=TextUtility.parseIntegerObj(memberForm.getUserRecordID());
      Integer userRoleID=TextUtility.parseIntegerObj(memberForm.getUserRoleID());
      //Integer userGroupID=TextUtility.parseIntegerObj(memberForm.getUserGroupID());
      String[] userGroupIDArray = TextUtility.splitString(memberForm.getUserGroupID(), ",");
      for (int i=0;i<userGroupIDArray.length;i++){
      	Integer userGroupID=TextUtility.parseIntegerObj(userGroupIDArray[i]);
      	if(UserMember.MEMBER_TYPE_USER.equals(memberForm.getMemberType())){
          MtmUserRecordUserGroupDAObject mtmUserRecordUserGroupDAO=new MtmUserRecordUserGroupDAObject(sessionContainer,conn);
          MtmUserRecordUserGroup mtmUserRecordUserGroup=new MtmUserRecordUserGroup();
          mtmUserRecordUserGroup.setUserRecordID(userRecordID);
          mtmUserRecordUserGroup.setUserGroupID(userGroupID);
          List list=mtmUserRecordUserGroupDAO.getListByUserRecordID(userRecordID,userGroupID);
          if(list.size()>0){
            request.setAttribute("error",UserErrorConstant.DUPLICATE_RECORD); 
          }else{
            mtmUserRecordUserGroupDAO.insertObject(mtmUserRecordUserGroup);
          }
        }else if(UserMember.MEMBER_TYPE_ROLE.equals(memberForm.getMemberType())){
          MtmUserGroupUserRoleDAObject mtmUserGroupUserRoleDAO=new MtmUserGroupUserRoleDAObject(sessionContainer,conn);
          MtmUserGroupUserRole mtmUserGroupUserRole=new MtmUserGroupUserRole();
          mtmUserGroupUserRole.setUserGroupID(userGroupID);
          mtmUserGroupUserRole.setUserRoleID(userRoleID);
          List list=mtmUserGroupUserRoleDAO.getByUserGroupIDUserRoleID(userGroupID,userRoleID);
          if(list.size()>0){
            request.setAttribute("error",UserErrorConstant.DUPLICATE_RECORD); 
          }else{
            mtmUserGroupUserRoleDAO.insertObject(mtmUserGroupUserRole);
          }
        }
      }
    }
  }
  
  public void removeRecord(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    SessionContainer sessionContainer=this.getSessionContainer(request);
    Connection conn=this.getConnection(request);
    MtmUserRecordUserGroupDAObject mtmUserRecordUserGroupDAO=new MtmUserRecordUserGroupDAObject(sessionContainer,conn); 
    MtmUserRecordUserRoleDAObject mtmUserRecordUserRoleDAO=new MtmUserRecordUserRoleDAObject(sessionContainer,conn);
    MtmUserGroupUserRoleDAObject mtmUserGroupUserRoleDAO=new MtmUserGroupUserRoleDAObject(sessionContainer,conn);
    ListUserMemberForm memberForm=(ListUserMemberForm)form;
    if(!Utility.isEmpty(memberForm.getBasicSelectedID())){
      String[] selectValue=memberForm.getBasicSelectedID();
      for(int i=0;i<selectValue.length;i++){
        Integer ID=TextUtility.parseIntegerObj(selectValue[i].substring(0,selectValue[i].indexOf("||")));
        String memType=selectValue[i].substring(selectValue[i].indexOf("||")+2,selectValue[i].length());
        if(UserMember.MEMBER_TYPE_USER.equals(memberForm.getMemberType())){
          Integer userRecordID=TextUtility.parseIntegerObj(memberForm.getUserRecordID());
          if(UserMember.MEMBER_TYPE_GROUP.equals(memType)){
            mtmUserRecordUserGroupDAO.deleteListByUserRecordID(userRecordID,ID); 
          }else if(UserMember.MEMBER_TYPE_ROLE.equals(memType)){
            mtmUserRecordUserRoleDAO.deleteListByUserRecordID(userRecordID,ID);
          }
        }else if(UserMember.MEMBER_TYPE_GROUP.equals(memberForm.getMemberType())){
          Integer userGroupID=TextUtility.parseIntegerObj(memberForm.getUserGroupID());
          if(UserMember.MEMBER_TYPE_USER.equals(memType)){
            mtmUserRecordUserGroupDAO.deleteListByUserRecordID(ID,userGroupID); 
          }else if(UserMember.MEMBER_TYPE_ROLE.equals(memType)){
            mtmUserGroupUserRoleDAO.deleteListByUserGroupID(userGroupID,ID);
          } 
        }else if(UserMember.MEMBER_TYPE_ROLE.equals(memberForm.getMemberType())){
          Integer userRoleID=TextUtility.parseIntegerObj(memberForm.getUserRoleID());
          if(UserMember.MEMBER_TYPE_USER.equals(memType)){
            mtmUserRecordUserRoleDAO.deleteListByUserRecordID(ID,userRoleID); 
          }else if(UserMember.MEMBER_TYPE_GROUP.equals(memType)){
            mtmUserGroupUserRoleDAO.deleteListByUserGroupID(ID,userRoleID);
          }  
        }        
      }
    }
  }
  public void getExcelReport(ActionMapping mapping, AbstractSearchForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
  	SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
  	ListUserMemberForm memberForm=(ListUserMemberForm)form;
  	MtmUserRecordUserRoleDAObject mtmUserRecordUserRoleDAO=new MtmUserRecordUserRoleDAObject(sessionContainer,conn);
    MtmUserGroupUserRoleDAObject mtmUserGroupUserRoleDAO=new MtmUserGroupUserRoleDAObject(sessionContainer,conn);
    MtmUserRecordUserGroupDAObject mtmUserRecordUserGroupDAO=new MtmUserRecordUserGroupDAObject(sessionContainer,conn);
    UserRecordDAObject userRecordDAO=new UserRecordDAObject(sessionContainer,conn);
    UserGroupDAObject userGroupDAO=new UserGroupDAObject(sessionContainer,conn);
    UserRoleDAObject userRoleDAO=new UserRoleDAObject(sessionContainer,conn);
    RootRetrievalManager rootRetrievalManager = new RootRetrievalManager(sessionContainer, conn);
    String role=UserMember.MEMBER_TYPE_ROLE;
    String group=UserMember.MEMBER_TYPE_GROUP;
    try{
  	/* creating excel file */
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("User Member List");
    sheet.setDefaultColumnWidth( (short) 18);
    short rowNum = 0;
    HSSFRow row = sheet.createRow(rowNum++);
    /* The label row */
    createCell(wb, row, (short) 0, MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "user.columnheader.member_name"));
    createCell(wb, row, (short) 1, MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "user.columnheader.member_desc"));
    createCell(wb, row, (short) 2, MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "user.columnheader.member_type"));
    createCell(wb, row, (short) 3, MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "common.columnheader.update_date"));
    createCell(wb, row, (short) 4, MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "common.columnheader.updater"));
    row = sheet.createRow(rowNum++);
    if(UserMember.MEMBER_TYPE_ROLE.equals(memberForm.getMemberType())){
        List userRoleList=new ArrayList();
        userRoleList.add(0,TextUtility.parseIntegerObj(memberForm.getUserRoleID()));
        List userRecordList=mtmUserRecordUserRoleDAO.getListByUserRoleAry(userRoleList,GlobalConstant.RECORD_STATUS_ACTIVE); 
        
        for(int i=0;i<userRecordList.size();i++){
          MtmUserRecordUserRole mtmUserRecordUserRole=(MtmUserRecordUserRole)userRecordList.get(i);
          UserRecord userRecord=(UserRecord)userRecordDAO.getObjectByID(mtmUserRecordUserRole.getUserRecordID());
          if(!Utility.isEmpty(userRecord)){
          	 createCell(wb, row, (short) 0, userRecord.getFullName());
          	 createCell(wb, row, (short) 1, "");
          	 createCell(wb, row, (short) 2, MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "user.label.member_type_user"));
          	 createCell(wb, row, (short) 3, TextUtility.formatTimestamp(userRecord.getUpdateDate()));
          	 createCell(wb, row, (short) 4, userRecord.getUpdaterName());
          	 row = sheet.createRow(rowNum++);
          }
        }
        List userGroupList=mtmUserGroupUserRoleDAO.getListByUserRoleAry(userRoleList,GlobalConstant.RECORD_STATUS_ACTIVE);
        for(int j=0;j<userGroupList.size();j++){     
          MtmUserGroupUserRole mtmUserGroupUserRole=(MtmUserGroupUserRole)userGroupList.get(j);  
          UserGroup userGroup=null;
          try{
            userGroup=(UserGroup)userGroupDAO.getObjectByID(mtmUserGroupUserRole.getUserGroupID());
          }catch(ApplicationException apx){
            if(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR.equals(apx.getMsgCode())){
              userGroup=null;
            }else{
              throw apx; 
            }
          }
          if(!Utility.isEmpty(userGroup)){
          	 createCell(wb, row, (short) 0, userGroup.getGroupName());
         	 createCell(wb, row, (short) 1, "");
         	 createCell(wb, row, (short) 2, MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "user.label.member_type_group"));
         	 createCell(wb, row, (short) 3, TextUtility.formatTimestamp(userGroup.getUpdateDate()));
         	 createCell(wb, row, (short) 4, userGroup.getUpdaterName());
         	 row = sheet.createRow(rowNum++);
          }
        }  
        request.getSession().setAttribute("ROLE",role);
      }else if(UserMember.MEMBER_TYPE_GROUP.equals(memberForm.getMemberType())){
        List userGroupList=new ArrayList();
        userGroupList.add(0,TextUtility.parseIntegerObj(memberForm.getUserGroupID()));
        List userRecordList=mtmUserRecordUserGroupDAO.getListByUserGroupAry(userGroupList,GlobalConstant.RECORD_STATUS_ACTIVE);
        
        for(int i=0;i<userRecordList.size();i++){
          MtmUserRecordUserGroup mtmUserRecordUserGroup=(MtmUserRecordUserGroup)userRecordList.get(i);
          UserRecord userRecord=(UserRecord)userRecordDAO.getObjectByID(mtmUserRecordUserGroup.getUserRecordID());
          if(!Utility.isEmpty(userRecord)){
          	 createCell(wb, row, (short) 0, userRecord.getFullName());
         	 createCell(wb, row, (short) 1, "");
         	 createCell(wb, row, (short) 2, MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "user.label.member_type_user"));
         	 createCell(wb, row, (short) 3, TextUtility.formatTimestamp(userRecord.getUpdateDate()));
         	 createCell(wb, row, (short) 4, userRecord.getUpdaterName());
         	 row = sheet.createRow(rowNum++);
          }
        } 
        request.getSession().setAttribute("GROUP",group);
      }
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();      
    wb.write(byteOut); 
    byte[] excelData = byteOut.toByteArray();
    
    DmsLocMaster locMaster = rootRetrievalManager.getLocMasterOfPersonalRootByOwnerID(sessionContainer.getUserRecordID());
    String locMasterPath = locMaster.getLocPath();
    
    String path = locMasterPath+"/excel"+Math.random()+".xls";
    FileOutputStream f=new FileOutputStream(path);
    InputStream in = null;
    try {
      in = new ByteArrayInputStream(excelData);        
      int c;
      while ( (c = in.read()) != -1) {
        f.write(c);
      }
    } catch (Exception e) {
      log.error(e);
    } finally {
      try {
        in.close();
      } catch (Exception ignore) {} finally {
        in = null;
      }
      try {
        f.close();
      } catch (Exception ignore) {} finally {
        f = null;
      }
     }
    request.getSession().setAttribute("excelFileName",path);
    byteOut.close();
    sessionContainer = null;
    rootRetrievalManager.release();
    } catch(Exception e){
      log.error("Member List error.", e);
    } finally {
      conn = null;
    }
    
  }
  
  private static void createCell(HSSFWorkbook wb, HSSFRow row, short column,String value) {
    HSSFCell cell = row.createCell(column);
    cell.setEncoding(cell.ENCODING_UTF_16);
    cell.setCellValue(value);
    //HSSFCellStyle cellStyle = wb.createCellStyle();
    //cellStyle.setAlignment(align);
    //cell.setCellStyle(cellStyle);
  }

}
